package com.bootlever.framework.redis.templates;

import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisSentinelPool;

@Configuration
public class RedisTemplateConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(RedisTemplateConfiguration.class);

    @Autowired(required = false)
    private JedisPool jedisPool;

    @Autowired(required = false)
    private JedisSentinelPool jedisSentinelPool;

    @Autowired(required = false)
    private JedisCluster jedisCluster;

    @Autowired(required = false)
    private RedissonClient redissonClient;

    @Bean
    public RedisTemplate jedisTemplate() {
        RedisTemplate redisTemplate = new RedisTemplate();
        if (jedisPool != null) {
            //单机模式
            redisTemplate.setJedis(jedisPool.getResource());
            logger.info("初始化jedis单机模式");
            //return redisTemplate;
        } else if (jedisCluster != null) {
            //集群模式
            redisTemplate.setJedis(jedisCluster);
            logger.info("初始化jedis集群模式");
            //return redisTemplate;
        } else if (jedisSentinelPool != null){
            redisTemplate.setJedis(jedisSentinelPool.getResource());
            logger.info("初始化jedis哨兵模式");
            //return redisTemplate;
        }
        logger.info("未初始化jedis");
        if (redissonClient != null) {
            redisTemplate.setRedissonClient(redissonClient);
        }
        return redisTemplate;
    }
}
